from torchsummary import summary

from torch import nn


class AlexNet(nn.Module):
    def __init__(self, num_classes=10, last_fc_size=4096):
        super().__init__()
        self.features = nn.Sequential(
            # 227*227*3
            nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0),
            # 55*55*96
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            # 27*27*96
            nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
            # 27*27*256
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            # 13*13*256
            nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
            # 13*13*384
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
            # 13*13*384
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
            # 13*13*256
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.fc = nn.Sequential(
            # 6*6*256=9216
            nn.Linear(9216, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(4096, last_fc_size),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(last_fc_size, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x


def show_model():
    net = AlexNet()
    summary(net, (3, 227, 227), batch_size=4096)


if __name__ == '__main__':
    show_model()
